var api = require('./../../config/api.js');
var app = getApp();
var util = require('./../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    toDoNum: 0,
    knightInfo: {},
    userInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ userInfo: app.globalData.userInfo})
  },

  loadInfo: function(){
    var that = this;
    var url = api.KnightPageInfo + '/' + app.globalData.userInfo.token;
    util.myRequest(url, false, function (data) {
      that.setData({ toDoNum: data.data.toDoNum, knightInfo: data.data.knight })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.globalData.shareInfo;
  },

  /**
   * 个人信息
   */
  goUserInfo: function () {
    wx.navigateTo({
      url: './../userInfo/userInfo?next=back'
    })
  },

  /**
   * 待完成
   */
  toWork: function () {
    wx.navigateTo({
      url: './../toDo/toDo'
    })
  },

  /**
   * 已完成
   */
  hasDone: function () {
    wx.navigateTo({
      url: './../done/done'
    })
  },

  /**
   * 钱包
   */
  toWallet: function () {
    wx.navigateTo({
      url: './../wallet/wallet'
    })
  },
  /**
   * 任务大厅
   */
	toTaskHall: function () {
		wx.navigateTo({
		  url: './../taskHall/taskHall'
		})
	},
  /**
   * 自动派单
   */
  autoOrder: function (e) {
    console.log('switch1 发生 change 事件，携带值为', e.detail.value)
    var that = this;
    var operate = '';
    var operateNum = 0;
    var cancelNum = 1;
    if (e.detail.value){
      operate = '开启';
      operateNum = 1;
      cancelNum =0;
    }else{
      operate = '关闭';
      operateNum = 0;
      cancelNum = 1;
    }
    wx.showModal({
      title: '提示',
      content: '确定要' + operate +'自动接单吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.changeAuto(operateNum);

        } else if (res.cancel) {
          console.log('用户点击取消')
          var info = that.data.knightInfo;
          info.kAutoTask = cancelNum;
          that.setData({ knightInfo: info});
        }
      }
    })
  },

  /**
   * 任务时间
   */
  goSchedule: function (e) {
    wx.navigateTo({
      url: './../schedule/schedule'
    })
  },

  changeAuto: function(swich){

    var postData = {};
    postData.token = app.globalData.userInfo.token;
    postData.autoTask = swich;

    util.myRequest(api.SetKnightInfo, postData, function(data){});
  },

  /**
   * 升级
   */
  goApply: function (e) {
    console.log(app.globalData.knightApply);
    if (app.globalData.knightApply){
      console.log(app.globalData.knightApply.kaStatus);
      if (app.globalData.knightApply.kaStatus == 0){
        wx.navigateTo({
          url: './../wait/wait'
        })
        return;
      } else if (app.globalData.knightApply.kaStatus == 2){
        wx.navigateTo({
          url: './../refuse/refuse'
        })
        return;
      }
    }

    wx.navigateTo({
      url: './../apply/apply'
    })
  },

  /**
   * 到用户页
   */
  goIndex: function (e) {
    // wx.switchTab({
    //   url: './../index/index'
    // })
    wx.switchTab({
      url: './../mine/mine'
    })
  },

  /**
   * 到选择地址
   */
  goSite: function (e) {
    wx.navigateTo({
      url: './../address/site/site'
    })
  },

})